package controllers;

import views.html.*;
import play.db.jpa.Transactional;
import dataaccess.BaseDao;
import play.*;
import play.mvc.*;
import model.Task;
import model.Users;
import model.Project;
import dataaccess.TaskDataAccess;
import dataaccess.UsersDataAccess;
import dataaccess.ProjectDataAccess;
import dataaccess.UsersDataAccess;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import model.Task;
import views.html.*;
import play.db.jpa.Transactional;
import play.mvc.Http.*;
import play.data.*;
import static play.data.Form.*;

@Security.Authenticated(SeguridadTareas.class)
public class Tareas extends Controller {

    public static Result index() {
        return ok(index.render(TaskDataAccess.getAll()));
    }

    @play.db.jpa.Transactional
    public static Result listarTareas() {
        long user_id = Long.parseLong(session().get("user_id"), 10);
        List<Task> tareas = UsersDataAccess.getById(user_id).getActiveTasks();
        return ok(userListarTareas.render(tareas));
    }

    @play.db.jpa.Transactional
    public static Result userVerTarea(long idTarea) {
        Task t = TaskDataAccess.getById(idTarea);
        return ok(userVerTarea.render(t));
    }
    
    @play.db.jpa.Transactional
    public static Result completarTarea(){
        DynamicForm form = form().bindFromRequest();
        long idTarea = Long.parseLong(form.get("idTarea"), 10);
        long cantHoras = Long.parseLong(form.get("duracionTarea"));
        Task t = TaskDataAccess.getById(idTarea);
        t.setFinalHours(cantHoras);
        if(form.get("finalizado") != null){
            t.setComplete(true);
        }
        
        TaskDataAccess.save(t);
        Users usuario = t.getUser();
        return redirect(routes.Tareas.listarTareas());
    }
    
    @play.db.jpa.Transactional
    public static Result listarTareasFinalizadas(){
        long user_id = Long.parseLong(session().get("user_id"), 10);
        List<Task> tareas = UsersDataAccess.getById(user_id).getDoneTasks();
        return ok(userListarTareasFinalizadas.render(tareas));
    }
}
